<?php
namespace app\madmin\controller;
use app\madmin\model\SystemConfigModel;

/**
 * 配置控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190101
 */
class SystemConfig extends PublicBase{

	# 快捷配置方法
	public function showBase(){
        $SystemConfig = new SystemConfigModel;

		if(request()->isPost()){
            $ArrayConfig = input('post.');

            if(!empty($ArrayConfig)){
                foreach($ArrayConfig as $k => $v){
                    $Where = [];
                    $Where['config_name'] = $k;
                    $Where['config_show'] = 1;
                    $FindConfig = $SystemConfig->mInfo($Where, 'config_id,config_value');
                    if(!empty($FindConfig)){
                        if($FindConfig['config_value'] != $v){
                            $SystemConfig->mUpdate($Where, ['config_value'=>$v]);
                        }
                    }
                }
            }

            $this->getConfigList();

            $this->ShowData(1, lang('XtTsGxCg'));
        }else{
            $ListGroup = $this->getLexiconCache('CONFIG_TITLE');

            $ListConfig = $SystemConfig->mAll(['config_show'=>1], '*');

            $Group = [];
            $Lists = [];
            if(!empty($ListGroup)){
                foreach($ListGroup as $k => $v){
                    $GroupCount = $SystemConfig->mCount(['config_group'=>$v['content_value'], 'config_show'=>1]);
                    if($GroupCount > 0){
                        $Group[$k]['group_type'] = $v['content_value'];
                        $Group[$k]['group_name'] = $v['content_name'];
                    }
                }

                foreach($Group as $k => $v){
                    $Lists[$k] = $v;
                    foreach($ListConfig as $i => $z){
                        if($v['group_type'] == $z['config_group']){
                            if($z['config_type'] == 2){
                                if(!empty($z['config_select'])){
                                    $ListConfig[$i]['config_select'] = explode(',', $z['config_select']);
                                }
                            }
                            $Lists[$k]['group_list'][] = $z;
                            unset($z);
                        }
                    }
                }
            }

            return $this->fetch('SystemConfig/showBase', [
                'Group' => $Group,
                'Lists' => $Lists
            ]);
        }
	}

	# 配置列表方法
    public function showList(){
        $SystemConfig = new SystemConfigModel;

        $Where = $this->getFormWhere();

        $Lists = $SystemConfig->mList($Where, '*', config('CNF_LISTPAGE'));

        return $this->fetch('SystemConfig/showList', [
            'Lists' => $Lists,
            'Group' => $this->getLexiconCache('CONFIG_TITLE')
        ]);
    }

    # 配置新增方法
    public function showAdd(){
        $SystemConfig = new SystemConfigModel;

        if(request()->isPost()){
            $Param = input('post.');

            $Back = $SystemConfig->mAdd($Param);

            $this->getConfigList();

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }else{
            return $this->fetch('SystemConfig/showAdd', [
                'Info'   => Null,
                'Group'  => $this->getLexiconCache('CONFIG_TITLE')
            ]);
        }
    }

    # 配置更新方法
    public function showEdit(){
        $SystemConfig = new SystemConfigModel;

    	$ID = input('param.id');

        if(request()->isPost()){
            $Param = input('post.');

            $Back = $SystemConfig->mEdit(['config_id'=>$ID], $Param);

            $this->getConfigList();

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }else{
            $Info = $SystemConfig->mInfo(['config_id'=>$ID]);

            if(empty($Info)){
                $this->ShowData(2, lang('XtCwCxCw'));
            }

            return $this->fetch('SystemConfig/showAdd', [
                'Info'   => $Info,
                'Group'  => $this->getLexiconCache('CONFIG_TITLE')
            ]);
        }
    }

    # 配置删除方法
    public function showDel(){
        if(request()->isPost()){
            $SystemConfig = new SystemConfigModel;

            $ID = input('post.id');

            $Back = $SystemConfig->mDel(['config_id'=>$ID]);

            $this->getConfigList();

            $this->ShowData($Back['code'], $Back['msgs']);
        }
    }

}